import { _decorator, Component, Input, EventMouse,input,Vec3, instantiate, Prefab ,Node,Collider2D,Contact2DType,IPhysics2DContact,Animation} from 'cc';
const { ccclass, property } = _decorator;

enum ShootType {
    OneShoot,
    TwoShoot,
    None
}

@ccclass('Player')
export class Player extends Component {

    @property
    shootRate:number = 0.5;

    @property(Prefab)
    bullet1Prefab:Prefab = null;

    @property(Node)
    bulletParent:Node = null;

    @property(Node)
    bullet1Position:Node = null;

    @property(Prefab)
    bullet2Prefab:Prefab = null;

    @property(Node)
    bullet2Position1:Node = null;

    @property(Node)
    bullet2Position2:Node = null;

    @property(Animation)
    anim: Animation = null;

    @property
    animHit: string = "";

    @property
    animDown: string = "";

    shootType: ShootType = ShootType.TwoShoot;

    shootTimer: number = 0;

    collider: Collider2D = null;

    @property
    hp:number = 3;

    isInvincible: boolean = false;

    inVincibleTimer:number = 0;

    inVincibleTime:number = 2;

    protected onLoad(): void {
        input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }
    protected onDestroy(): void {
        input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
        if (this.collider) {
            this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
          }
    }

    start() {
        // 注册单个碰撞体的回调函数
        this.collider = this.getComponent(Collider2D);
        if (this.collider) {
          this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
      }

    onMouseMove(event: EventMouse) {
        if(this.hp <= 0) return;
        const p = this.node.position
        const targetPosition = new Vec3(p.x+event.getDeltaX(),p.y+event.getDeltaY(),p.z)
        if (targetPosition.x < -230) {
            targetPosition.x = -230 
        }

        if (targetPosition.y < -380) {
            targetPosition.y = -380 
        }

        if (targetPosition.y > 380) {
            targetPosition.y = 380 
        }

        if (targetPosition.x > 230) {
            targetPosition.x = 230 
        }

        this.node.setPosition(targetPosition)
    }

    update(dt: number): void {
        switch(this.shootType){
            case ShootType.OneShoot:
                this.oneShoot(dt)
                break;
            case ShootType.TwoShoot:
                this.twoShoot(dt)
                break;
        }

        if(this.isInvincible){
            this.inVincibleTimer += dt
            if(this.inVincibleTimer >= this.inVincibleTime){
                this.isInvincible = false;
                this.inVincibleTimer = 0;
            }
        }
        
    }

    oneShoot(dt:number){
        this.shootTimer += dt;
        if (this.shootTimer >= this.shootRate) {
            this.shootTimer = 0;
            const bullet1 = instantiate(this.bullet1Prefab);
            this.bulletParent.addChild(bullet1);
            bullet1.setWorldPosition(this.bullet1Position.worldPosition);
        }
    }

    twoShoot(dt:number){
        this.shootTimer += dt;
        if (this.shootTimer >= this.shootRate) {
            this.shootTimer = 0;
            const bullet1 = instantiate(this.bullet2Prefab);
            const bullet2 = instantiate(this.bullet2Prefab);
            this.bulletParent.addChild(bullet1);
            this.bulletParent.addChild(bullet2);
            bullet1.setWorldPosition(this.bullet2Position1.worldPosition);
            bullet2.setWorldPosition(this.bullet2Position2.worldPosition);
        }
    }


    onBeginContact(
        selfCollider: Collider2D,
        otherCollider: Collider2D,
        contact: IPhysics2DContact | null
      ) {
        if(this.isInvincible) return;
        this.isInvincible = true;
        this.inVincibleTimer = 0;
        // 只在两个碰撞体开始接触时被调用一次
        this.hp--;
        // otherCollider.enabled = false;
    
        if (this.hp <= 0) {
            this.shootType = ShootType.None;
            this.anim.play(this.animDown);
            this.node.destroy();
          if (this.collider) {
            this.collider.enabled = false;
          }
        }else{
            this.anim.play(this.animHit);
        }
      }
}


